/*
 * Преобразование π-функции в z-функцию
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int *
replay_pi_func(int *pi, const int n)
{
	int *string;
	register int i = 0;
	int next_c = 1;

	string = (int *) malloc(n * sizeof *string);

	while (i < n) {
		if (pi[i] == 0)
			string[i] = next_c++;
		else
			string[i] = string[pi[i] - 1];
		i++;
	}

	return string;
}

int *
z_func(const int *s, const int n)
{
	int i, l, r;
	int *z;

#ifndef MIN
#define MIN(a, b)	((a) < (b) ? (a) : (b))
#endif

	z = (int *) malloc(n * sizeof *z);
	memset(z, 0, n * sizeof *z);

	for (i = 1, l = 0, r = 0; i < n; ++i) {
		if (i <= r)
			z[i] = MIN(r - i + 1, z[i - l]);
		while (i + z[i] < n && s[z[i]] == s[i + z[i]])
			++z[i];
		if (i + z[i] - 1 > r)
			l = i, r = i + z[i] - 1;
	}

	fprintf(stderr, "z[0]=%d, n=%d\n", z[0], n);
	z[0] = n;

	return z;
}

int
main(void)
{
	int *z, *pi, n,
	    *string;
	int i = 0;

	scanf("%d", &n);
	pi = (int *) malloc(n * sizeof *pi);

	while (i < n)
		scanf("%d", pi + i++);

	string = replay_pi_func(pi, n);
	z = z_func(string, n);

	i = -1;
	while (++i < n)
		printf("%d%c", z[i], (i < n - 1) ? ' ' : '\n');

	free(z); free(string); free(pi);
	return 0;
}
